package
{
	import events.SlideObjectEvent;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;
	
	import layouts.ILayout;
	
	import mx.core.Container;
	import mx.utils.ObjectProxy;
	
	public class Slide
	{
		public var slideThumb:ILayout;
		public var slideControl:ILayout;

		public function Slide()
		{
			_slideData = new ObjectProxy();
		}
		
		private var _slideData:ObjectProxy;
		public function get slideData():ObjectProxy{
			return _slideData;
		}
		public function set slideData(value:ObjectProxy):void{
			_slideData = value;
			if(slideThumb){
				slideThumb.slideData = _slideData;
			}
			if(slideControl){
				slideControl.slideData = _slideData;
			}
		}
		
		public function updateObject(objectId:String, objectData:ObjectProxy):void{
			var newData:ObjectProxy = slideData;
			if(!newData.objects){
				newData.objects = new ObjectProxy();
			}
			newData.objects[objectId] = objectData;
			slideData = newData;
		}
		
		private function unsetLayout():void{
			if(slideControl){
				(slideControl as Container).parent.removeChild(slideControl as DisplayObject);
				slideControl = null;
			}
		}
		
		public function setLayout(layoutClassName:String, onSlideObjectClick:Function):void{
			slideData.layout = layoutClassName;
			unsetLayout();
			
			var layoutRef:Class = getDefinitionByName(layoutClassName) as Class;
			
			var control:Container = new layoutRef() as Container;
			control.addEventListener(SlideObjectEvent.SLIDE_OBJECT_CLICK, onSlideObjectClick, false, 0, true);
			slideControl = control as ILayout;
			slideControl.slideData = slideData;
		}
	}
}